package com.ruoyi.web.controller.product;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.shop.common.domain.CommonPage;
import com.ruoyi.shop.common.utils.BeanConvertUtils;
import com.ruoyi.shop.modules.domain.vo.ProductVo;
import com.ruoyi.shop.nosql.es.domain.EsProduct;
import com.ruoyi.shop.nosql.es.domain.EsProductRelatedInfo;
import com.ruoyi.shop.nosql.es.service.EsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索商品管理Controller
 */
@RestController
@Api(tags = "EsProductController")
@Tag(name = "EsProductController", description = "搜索商品管理")
@RequestMapping("/esProduct")
public class EsProductController extends BaseController {
    @Autowired
    private EsProductService esProductService;

    @ApiOperation(value = "导入所有数据库中商品到ES")
    @PostMapping(value = "/importAll")
    public AjaxResult importAllList() {
        int count = esProductService.importAll();
        return AjaxResult.success(count);
    }

    @ApiOperation(value = "根据id删除商品")
    @GetMapping(value = "/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        esProductService.delete(id);
        return AjaxResult.success(null);
    }

    @ApiOperation(value = "根据id批量删除商品")
    @PostMapping(value = "/delete/batch")
    public AjaxResult delete(@RequestParam("ids") List<Long> ids) {
        esProductService.delete(ids);
        return AjaxResult.success(null);
    }

    @ApiOperation(value = "根据id创建商品")
    @PostMapping(value = "/create/{id}")
    public AjaxResult create(@PathVariable Long id) {
        EsProduct product = esProductService.create(id);
        if (product != null) {
            return AjaxResult.success(product);
        } else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "简单搜索")
    @GetMapping(value = "/search/simple")
    public AjaxResult search(@RequestParam(required = false) String keyword,
                             // 搜索关键词（可选）
                             @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                             // 页码（可选，默认为0）
                             @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        // 调用esProductService的search方法，传入关键词、页码和每页显示数量，返回EsProduct的分页结果
        Page<EsProduct> esProductPage = esProductService.search(keyword, pageNum, pageSize);
        // 将EsProduct的分页结果转换为CommonPage对象，并包装成AjaxResult对象返回
        return AjaxResult.success(CommonPage.restPage(esProductPage));
    }


    @ApiOperation(value = "综合搜索、筛选、排序")
    // 排序字段的API隐式参数
    @ApiImplicitParam(name = "sort", value = "排序字段:0->按相关度；1->按新品；2->按销量；3->价格从低到高；4->价格从高到低",
            defaultValue = "0", allowableValues = "0,1,2,3,4", paramType = "query", dataType = "integer")
    @GetMapping(value = "/search")
    public TableDataInfo search(@RequestParam(required = false) String keyword,
                                // 品牌ID的API请求参数
                                @RequestParam(required = false, defaultValue = "0") Long brandId,
                                // 商品分类ID的API请求参数
                                @RequestParam(required = false, defaultValue = "0") Long productCategoryId,
                                // 页码的API请求参数，默认为0
                                @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                // 每页显示数量的API请求参数，默认为5
                                @RequestParam(required = false, defaultValue = "8") Integer pageSize,
                                // 排序方式的API请求参数，默认为0
                                @RequestParam(required = false, defaultValue = "0") Integer sort) {
        pageNum -= 1;
        // 调用esProductService的search方法，传入搜索关键词、品牌ID、商品分类ID、页码、每页显示数量和排序方式，返回EsProduct的分页结果
        Page<EsProduct> esProductPage = esProductService.search(keyword, brandId, productCategoryId, pageNum, pageSize, sort);
        // 将EsProduct的分页结果转换为TableDataInfo对象
        List<EsProduct> esProductList = esProductPage.getContent();
        // 调用ProductUtils工具类的convertProductsToVos方法，将EsProduct列表转换为ProductVo列表
        List<ProductVo> productVos = BeanConvertUtils.convertProductsToVos(esProductList, EsProduct::getAlbumPics);
        // 将EsProduct的分页结果转换为TableDataInfo对象
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(esProductPage.getTotalElements());
        tableDataInfo.setRows(productVos);
        return tableDataInfo;

    }


    @ApiOperation(value = "根据商品id推荐商品")
    @GetMapping(value = "/recommend/{id}")
    public AjaxResult recommend(@PathVariable Long id,
                                @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<EsProduct> esProductPage = esProductService.recommend(id, pageNum, pageSize);
        return AjaxResult.success(CommonPage.restPage(esProductPage));
    }

    @ApiOperation(value = "获取搜索的相关品牌、分类及筛选属性")
    @GetMapping(value = "/search/relate")
    public AjaxResult searchRelatedInfo(@RequestParam(required = false) String keyword) {
        EsProductRelatedInfo productRelatedInfo = esProductService.searchRelatedInfo(keyword);
        return AjaxResult.success(productRelatedInfo);
    }
    // 搜索提示功能
    @ApiOperation(value = "获取搜索提示")
    @GetMapping(value = "/search/suggest")
    public AjaxResult searchSuggest(@RequestParam(required = false) String keyword) {
        List<String> keywordsList = esProductService.searchSuggest(keyword);
        return AjaxResult.success(keywordsList);
    }

    //猜你喜欢
//    @ApiOperation(value = "获取猜你喜欢")
//    @GetMapping(value = "/guess/like")
//    public AjaxResult guessLike(@RequestParam(required = false) Long id,
//                               @RequestParam(required = false, defaultValue = "0") Integer pageNum,
//                               @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
//        Page<EsProduct> esProductPage = esProductService.guessLike(id, pageNum, pageSize);
//        return AjaxResult.success(CommonPage.restPage(esProductPage));
//    }
}
